# Set working directory to main replication folder
setwd("C:/Users/Eric/Dropbox/Book/analysis/replication/")

# Load packages
library(plyr)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(data.table)
library(zoo)

# Load function
source("./function_code/war_plots.R")


#### Read in data ####

# War-day data
d = fread("./data/wardata_book.csv")

# Negotiation start data
s = fread("./data/negstarts.csv")

# Battle data
bd = fread("./data/iwbd1.0.csv")


#### FIGURE 2.1: Negotiations in four wars ####
neg_AS = makeNegPlot("Austro-Sardinian")
neg_TCA = makeNegPlot("Third Central American")
neg_K = makeNegPlot("Korean")
neg_UT = makeNegPlot("Ugandian-Tanzanian")

fourNegs = grid.arrange(neg_AS, neg_TCA, neg_K, neg_UT, ncol=1)
ggsave("./figures/figure_2.1.pdf", plot=fourNegs, width=4.5, height=7)


#### TABLE 2.1: Frequency of negotiations across war-days ####
table(d$neg); round(prop.table(table(d$neg)), 3)
table(d$negInt); round(prop.table(table(d$negInt)), 3)
table(d$negExt); round(prop.table(table(d$negExt)), 3)


#### TABLE 2.2: Results of wartime negotiations ####

## Median lengths of negotiations ##
median(s$negLength)                        # All
median(s$negLength[which(s$pressure==0)])  # Internal
median(s$negLength[which(s$pressure==1)])  # External

## Whether negotiations ended wars ##
table(ifelse(s$endwar=="0", 1, 0))                                              # All, counts
round(prop.table(table(ifelse(s$endwar=="0", 1, 0))), 3)                        # All, proportions
table(ifelse(s$endwar[which(s$pressure==0)]=="0", 1, 0))                        # Internal, counts
round(prop.table(table(ifelse(s$endwar[which(s$pressure==0)]=="0", 1, 0))), 3)  # Internal, proportions
table(ifelse(s$endwar[which(s$pressure==1)]=="0", 1, 0))                        # External, counts
round(prop.table(table(ifelse(s$endwar[which(s$pressure==1)]=="0", 1, 0))), 3)  # External, proportions


#### FIGURE 2.2: Average trajectory of negotiations ####

# War names and sequence of proportions
wn = unique(d$warName)
rseq = seq(0, 1, 0.05)

# Calculate trajectories for all wars in the dataset
meansList = list()
for (i in 1:length(wn)) {
  oneWar = d[d$warName==wn[i],]
  mneg = mnegInt = mnegExt = maos = maoa = mapdiff = mnbat = mos = NA
  for (j in 1:length(rseq)) {
    part = oneWar[oneWar$propDoneR==rseq[j],]
    mneg[j] = mean(part$neg, na.rm=T)
    mnegInt[j] = mean(part$negInt, na.rm=T)
    mnegExt[j] = mean(part$negExt, na.rm=T)
    mos[j] = mean(part$outSum60, na.rm=T)
    maos[j] = mean(part$AOS60, na.rm=T)
    maoa[j] = mean(part$AOA)
    mapdiff[j] = mean(part$apropdiff, na.rm=T)
    mnbat[j] = mean(part$nBattle, na.rm=T)
  }
  meansList[[i]] = data.frame(post45 = oneWar$post45[1], propDone=rseq, 
                              neg=mneg, negInt=mnegInt, negExt=mnegExt, outSum60 = mos, AOS60=maos, AOA=maoa, apropdiff=mapdiff, nBattle = mnbat)
}
mr = rbindlist(meansList)
mr = as.data.frame(apply(mr, 2, na.approx, na.rm=F))
mr = na.omit(mr)
sm = ksmooth(mr$propDone, mr$neg, bandwidth=0.2)
smInt = ksmooth(mr$propDone, mr$negInt, bandwidth=0.2)
smExt = ksmooth(mr$propDone, mr$negExt, bandwidth=0.2)
smOS = ksmooth(mr$propDone, mr$outSum60, bandwidth=0.2)
smAOS = ksmooth(mr$propDone, mr$AOS60, bandwidth=0.2)
smAOA = ksmooth(mr$propDone, mr$AOA, bandwidth = 0.2)
smAPD = ksmooth(mr$propDone, mr$apropdiff, bandwidth=0.2)
smNB = ksmooth(mr$propDone, mr$nBattle, bandwidth=0.2)

traj = data.frame(post45 = mr$post45, propDone=sm$x, neg=sm$y, negInt=smInt$y, negExt=smExt$y, 
                  outSum60 = smOS$y, AOS60=smAOS$y, AOA=smAOA$y, apropdiff=smAPD$y, nBattle=smNB$y)
traj$propDone = round_any(traj$propDone, 0.05, floor)
traj = unique(traj)
traj = traj[!duplicated(traj$propDone),]

# Generate plot
traj |> pivot_longer(cols=contains("neg"), names_to="typeNeg", values_to="propNeg") |> 
  mutate(typeNeg = factor(typeNeg, levels=c("neg", "negInt", "negExt"))) |>
  ggplot(aes(x=propDone, y=propNeg)) + geom_line(aes(color=typeNeg), lineend="round", size=1.5) + theme_bw() + 
  theme(plot.title=element_text(size=10),
        axis.title.x = element_text(size = 9),
        axis.title.y = element_text(size = 9),
        legend.position="bottom") +
  xlab("Proportion of war elapsed") + ylab("Pr(Negotiation)") +
  scale_color_manual("", values=c("black", "gray35", "gray70"), labels=c("All", "Internal", "External")) + ylim(0,0.4) 
ggsave("./figures/figure_2.2.pdf", height=3.25, width=4)


#### TABLE 2.3: Distribution of battle victories ####
bd$victorWarLevel = factor(bd$victorWarLevel, levels=c("Initiator", "Inconclusive", "Target"))
bd$victorBattleLevel = factor(bd$victorBattleLevel, levels=c("Attacker", "Inconclusive", "Defender"))
table(bd$victorWarLevel, bd$victorBattleLevel)
round(prop.table(table(bd$victorWarLevel, bd$victorBattleLevel)), 3)


#### FIGURE 2.3: Four measures of the battlefield in World War I ####
makeFightPlot(war_name="World War I")
ggsave("./figures/figure_2.3.pdf", height=6.5, width=4.5)

######################################################################################################


#### APPENDIX MATERIALS FOR CHAPTER 2 ####

##### APPENDIX 1: MEASURES OF FIGHTING IN THE RUSSO-JAPANESE WAR #####

###### FIGURE A1: Five measures of the battlefield in the Russo-Japanese War ######
makeFightPlot("Russo-Japanese")
ggsave("./figures/figure_a1.pdf", height=6.5, width=4.5)

##### APPENDIX 2: DESCRIPTIVE STATISTICS FOR QUANTITATIVE DATA #####

###### TABLE A1: Summary statistics for continuous variables ######
d |> dplyr::select(AOS60, AOA, issues, cincP, nBellig, logBattles, nBattle, nBattleTrend) |> 
  reframe(across(everything(), function(x) {round(summary(x), 3)})) |> t()

###### TABLE A2: Summary statistics for binary variables (counts) ######
d |> dplyr::select(post45, neg, negInt, negExt, contig, polBinA, nuk, maj, minor, anyDipEnemy, keyAllies, ceasefire) |>
  reframe(across(everything(), table)) |> t()

###### TABLE A2: Summary statistics for binary variables (proportions) ######
d |> dplyr::select(post45, neg, negInt, negExt, contig, polBinA, nuk, maj, minor, anyDipEnemy, keyAllies, ceasefire) |>
  reframe(across(everything(), function(x) {round(prop.table(table(x)), 3)})) |> t()